#
# NASAL instruments for TU-154B
# Yurik V. Nikiforoff, yurik@megasignal.com
# Novosibirsk, Russia
# jun 2007
# Rebuild for Yak-40 by Mikhail Zuev april 2010

init_instruments = func {
    print("Initializing Nasal Instruments System");
	
    setprop("/instrumentation/airspeed-indicator/serviceable", 1);
	setprop("/instrumentation/altimeter/serviceable", 1);
	setprop("/instrumentation/inst-vertical-speed-indicator/serviceable", 1);
	setprop("/instrumentation/vertical-speed-indicator/serviceable", 1);
	setprop("/instrumentation/attitude-indicator/serviceable", 1);
	}
###########################
# RV-5M support
rv5m_handler = func{
settimer( rv5m_handler, 0.1 );
# Arretir:
if( getprop("Yak-40/instrumentation/rv-5m/caged-flag" ) != 0 )
	{
	setprop("Yak-40/instrumentation/rv-5m/warn", 0 );
	setprop("Yak-40/instrumentation/rv-5m/indicated-altitude-m", 0.0 );
	return;
	}
if( getprop("Yak-40/instrumentation/rv-5m/serviceable" ) != 1 ) 
	{
        setprop("Yak-40/instrumentation/rv-5m/warn", 0 );
        return;
	}
# get altitude and check if device is warmed
#var alt = getprop("fdm/jsbsim/instrumentation/indicated-altitude-m");
var alt = getprop("fdm/jsbsim/instrumentation/indicated-altitude-m");
var hot = getprop("Yak-40/instrumentation/rv-5m/hot");
if( alt == nil ) alt = 0.0;
if( hot == nil ) hot = 0.0;
if( alt < hot ) alt = hot;
interpolate("Yak-40/instrumentation/rv-5m/indicated-altitude-m", alt, 0.1 );
# check warning
var limit = getprop("Yak-40/instrumentation/rv-5m/index-m");
if( limit == nil ) return;
if( alt < limit ) 
	{
	setprop("Yak-40/instrumentation/rv-5m/warn", 1 );
#	interpolate("tu154/systems/electrical/indicators/radioaltimeter-limit", 1.0, 0.1);
	}
else { 
	setprop("Yak-40/instrumentation/rv-5m/warn", 0 );
#	interpolate("tu154/systems/electrical/indicators/radioaltimeter-limit", 0.0, 0.1);
	}
}

rv5m_power = func{
if( getprop( "Yak-40/switches/RV-5-1" ) == 1.0 )
	{
	setprop("Yak-40/instrumentation/rv-5m/hot", 5000.0 );
	}
else {
	setprop("Yak-40/instrumentation/rv-5m/hot", 0 );
	}
}

setlistener("Yak-40/switches/RV-5-1", rv5m_power,0,0);
setlistener("Yak-40/instrumentation/rv-5m/serviceable", rv5m_power,0,0);


rv5m_handler();
###########################
# Air speed indicator US-350
# 1 knots = 1.852 km/h
us = func {
    knots=getprop("/instrumentation/airspeed-indicator/indicated-speed-kt");
	alt = getprop( "/fdm/jsbsim/atmosphere/density-altitude" );
    if( knots != nil ){
    setprop("/Yak-40/instrumentation/kus-730/indicated-speed-kmh", knots*1.852-(0.0078*alt) );
    }
    settimer(us, 0);	# count speed every frame
   }
us ();

usis = func {
    knots=getprop("/instrumentation/airspeed-indicator/indicated-speed-kt");
    if( knots != nil ){
    setprop("/Yak-40/instrumentation/kus-730/true-speed-kmh", knots*1.852 );
    }
    settimer(usis, 0);	# count speed every frame
   }
usis ();
###########################
